package com.android.gilastv.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.Log;
import com.android.baselibrary.tool.SPUtil;
import com.android.baselibrary.util.GlideUtil;
import com.android.baselibrary.view.MultiStateView;
import com.android.gilastv.R;
import com.android.gilastv.bean.AppLoadingInfo;
import com.android.gilastv.bean.CommVideoInfo;
import com.android.gilastv.constants.SPConstants;
import com.android.gilastv.dialog.XieYi_DialogFragment;
import com.android.gilastv.manager.API_Manager;
import com.gyf.immersionbar.ImmersionBar;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/7/21.
 */

public class LoadingActivity extends BaseActivity {

    @BindView(R.id.iv_bg)
    ImageView iv_bg;

    Handler handler = new Handler();
    Context mContext;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_loading;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this).statusBarDarkFont(true).navigationBarDarkIcon(true).init();
        mContext = this;
        setContentView(R.layout.activity_loading);
        boolean hasOk = SPUtil.getBoolValue(mContext, SPConstants.HasAccept, false);
        if (hasOk) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    goToMain();
                }
            }, 2500);
        } else {
            XieYi_DialogFragment dialogFragment = (XieYi_DialogFragment) XieYi_DialogFragment.showDialog(getSupportFragmentManager(), null);
            dialogFragment.setCallback(new CommCallBack() {
                @Override
                public void onResult(Object obj) {
                    boolean result = (boolean) obj;
                    if (!result) {
                        Map<String, Object> params = new HashMap<>();
                        params.put("isSecond", true);
                        XieYi_DialogFragment dialogFragment1 = (XieYi_DialogFragment) XieYi_DialogFragment.showDialog(getSupportFragmentManager(), params);
                        dialogFragment1.setCallback(new CommCallBack() {
                            @Override
                            public void onResult(Object obj) {
                                boolean result = (boolean) obj;
                                if (result) {
                                    SPUtil.putValue(mContext, SPConstants.HasAccept, true);
                                    goToMain();
                                }
                            }
                        });
                    } else {
                        SPUtil.putValue(mContext, SPConstants.HasAccept, true);
                        goToMain();
                    }
                }
            });
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        API_Manager.appCover(mContext, new OkHttpCallBack<BaseResponce<AppLoadingInfo>>() {
            @Override
            public void onSuccess(BaseResponce<AppLoadingInfo> baseResponce) {
                GlideUtil.displayImage(mContext, CommVideoInfo.getVideoUrl(baseResponce.data.fileId), iv_bg, 0);
                Log.i("url: " + CommVideoInfo.getVideoUrl(baseResponce.data.fileId));
                Log.i("url: " + baseResponce.data.remark);
                TextView tv_text = findViewById(R.id.tv_text);
                tv_text.setText(baseResponce.data.remark);
            }

            @Override
            public void onFailure(BaseResponce<AppLoadingInfo> baseResponce) {
            }
        });
    }

    //跳首页
    private void goToMain() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        Intent intent;
//        if (UserManager.isLogin(mContext)) {
//            intent = new Intent(mContext, MainActivity.class);
//        } else {
//            intent = new Intent(mContext, LoginActivity.class);
//        }
        intent = new Intent(mContext, HomeActivity.class);
        mContext.startActivity(intent);
        overridePendingTransition(R.anim.main_activity_in, R.anim.splash_out);
        finish();
    }


}
